#include "vsserver.h"
#include "vssocket.h"
#include "talkdialog.h"
#include "startsettings.h"
#include <qdebug.h>
VSServer::VSServer(QObject *parent) :
	QTcpServer(parent),socket(0)
{
}
void VSServer::incomingConnection(int handle)
{
	qDebug()<<"incon";
	if(!socket)
	{
		socket = new VSSocket(this);
		socket->setSocketDescriptor(handle);
		connect(socket,SIGNAL(ready()),this,SLOT(onReady()));
		connect(socket,SIGNAL(sendMsg(QString)),this,SLOT(onmsg(QString)));
		connect(socket,SIGNAL(erred(QString)),this,SLOT(onmsg(QString)));
		connect(socket,SIGNAL(disconnected()),this,SLOT(onDisCon()));
		socket->sendWelcomeMessage();
		//connect(socket,SIGNAL(erred(QString)),StartSettings::talkDialog,SLOT(addMsg(QString)));
		//connect(socket,SIGNAL(talked(QString)),StartSettings::talkDialog,SLOT(addMsg(QString)));
		//connect(socket,SIGNAL(ready()),StartSettings::talkDialog,SLOT(onready()));
		if(StartSettings::socket)
		{
			delete StartSettings::socket;
			StartSettings::socket=0;
		}
		StartSettings::socket=socket;
	}else
	{
		VSSocket *s= new VSSocket(this);
		s->setSocketDescriptor(handle);
		s->sendErrorMessage(VSSocket::full);
		s->deleteLater();
	}
}
void VSServer::onReady()
{
	emit ready();
}
void VSServer::onmsg(QString s)
{
	emit sendMsg(s);
}
void VSServer::onDisCon()
{
	emit disCon();
}
